using PersonnelStatistics.BLL;
using PersonnelStatistics.DAL;

namespace 人员统计
{
    public partial class Form1 : Form
    {
        private readonly PersonalService _personalSevice;
        public Form1()
        {
            InitializeComponent();
            _personalSevice = new PersonalService();
            ReadConfig();
        }


        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button4.Enabled=true;
       
            var data=_personalSevice.GetList();
            this.dataGridView1.DataSource = data;
            toolStripStatusLabel1.Text = $"当前总计人数为：{data.Count}人";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 200;
            timer1.Enabled = true;
            timer1.Start();
        }
        /// <summary>
        /// 读取配置文件
        /// </summary>
        private void ReadConfig()
        {
            string path4 = System.IO.Directory.GetCurrentDirectory() + "\\set.ini";
            List<string> listLines = new List<string>();

            try
            {
                using (StreamReader reader = new StreamReader(path4))
                {
                    int i = 0;
                    string line = reader.ReadLine();
                    while (line != "" && line != null)
                    {
                        if (i <= 5)
                        {
                            listLines.Add(line);
                        }
                        line = reader.ReadLine();
                    }
                    //循环完后，listLines 里面就放有第三行到第十行的数据了
                }
                MyDbContextOptions.DataSource = listLines[0];
                MyDbContextOptions.DbAccount = listLines[1];
                MyDbContextOptions.Password = listLines[2];
                MyDbContextOptions.DbName = listLines[3];
                MyDbContextOptions.DataSource = listLines[0];

            }
            catch
            {

                MessageBox.Show("读取配置文件失败！软件将自动生成测试配置数据,请进入系统设置重新设置参数。", "配置文件读取错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                //读取数据后定义数据库内容和软件标题等

                this.Text = "Abc系统";
                label1.Text = "Abc系统";
                //主标题文字显示位置调整
                label1.Left = this.Width / 2 - label1.Width / 2;
                MessageBox.Show("欢迎使用人员管理系统测试版，软件请勿用作商业用途！软件功能添加或更新，请联系18390928303 微信同号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form form2=new Form2(); 
            form2.ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void button3_Click(object sender, EventArgs e)
        {
             Form form3 = new Form3();
            form3.ShowDialog();
             
        }

        private void button5_Click(object sender, EventArgs e)
        {
        }


        private void dataGridView1_Click(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void 打开数据库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button1_Click(null, null);
        }

        private void 退出ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 录入系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button2_Click(null, null);
        }

        private void 查询修改数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button3_Click(null, null);
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random ro = new Random(10);
            long tick = DateTime.Now.Ticks;
            Random ran = new Random();
            int R = ran.Next(255);
            int G = ran.Next(255);
            int B = ran.Next(255);
            B = (R + G > 400) ? R + G - 400 : B;
            B = (B > 255) ? 255 : B;
            label1.ForeColor = Color.FromArgb(R, G, B);
            label2.Text = DateTime.Now.ToString() ;
 
        }

        private void button5_Click_1(object sender, EventArgs e)
        {

     
        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if(dataGridView1.Rows.Count > 0)
            {

           
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            //saveFileDialog.CreatePrompt = true; //当文件不存在时是否提示创建新文件
            saveFileDialog.Title = "导出Excel文件";
            saveFileDialog.ShowDialog();
            if (saveFileDialog.FileName == "")
                return;
            Stream myStream;
            myStream = saveFileDialog.OpenFile();
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            string str = "";
            try
            {
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    if (i > 0)
                    {
                        str += "\t";
                    }
                    str += dataGridView1.Columns[i].HeaderText;
                }
                sw.WriteLine(str);
                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {
                    string tempStr = "";
                    for (int k = 0; k < dataGridView1.Columns.Count; k++)
                    {
                        if (k > 0)
                        {
                            tempStr += "\t";
                        }
                        tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
                    }
                    sw.WriteLine(tempStr);
                }
                sw.Close();
                myStream.Close();
                  }
             catch (Exception ex)
                     {
                      MessageBox.Show(ex.ToString());
                     }
              finally
               {
                     sw.Close();
                     myStream.Close();
                     MessageBox.Show("数据导出成功!");
                }
            }
            else
            {
                MessageBox.Show("表格不存在数据，请刷新数据或者读取数据库再操作！");
            }
        }

    private void 系统设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button4_Click(null, null);
        }

        private void 关于本软件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 aboutBox = new AboutBox1();
            aboutBox.ShowDialog();  

        }

        private void 系统设置ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form4 form = new Form4();
            form.ShowDialog();
       
        }
    }
}